package com.zl.tesseract.scanner.view;

import android.annotation.SuppressLint;
import android.content.Context;
import android.graphics.Rect;
import android.graphics.drawable.BitmapDrawable;
import android.os.Build;
import android.support.v7.widget.LinearLayoutManager;
import android.support.v7.widget.RecyclerView;
import android.view.Gravity;
import android.view.LayoutInflater;
import android.view.View;
import android.view.View.OnClickListener;
import android.widget.LinearLayout;
import android.widget.PopupWindow;
import android.widget.TextView;

import com.zl.tesseract.R;
import com.zl.tesseract.scanner.adapter.BrandListAdapter;
import com.zl.tesseract.scanner.beans.BrandBean;

import java.util.ArrayList;
import java.util.List;


/**
 * 选择年级pop.
 * @author: zhangyan
 * @date: 2018/7/30
 */
public class SelectBrandPopwidow extends PopupWindow {

	private Context mContext;
    private int popupWidth;
    private int popupHeight;

	/* 数据源 */
	private List<BrandBean> gradeList;
	/* 点击源 */
    private TextView tvSelectGrade;
    /* 结果 */
	private BrandBean resultBean;

	private BrandListAdapter mAdapter;

	/**
	 * 自定义的多选框
	 * @param context context
	 * @param gradeList 数据源
	 * @param defaultGrade defaultGrade
	 */
	public SelectBrandPopwidow(Context context, List<BrandBean> gradeList, TextView tvSelectGrade, String defaultGrade) {
		this.mContext = context;
		this.gradeList = gradeList;
		this.tvSelectGrade = tvSelectGrade;
		initMutiSelect();

		// 选中默认值
        for (BrandBean typeBean: gradeList) {
            if (defaultGrade.equals(typeBean.getId())){
                resultBean = typeBean;
                typeBean.setChecked(true);
            }
        }

	}

    /**
     * 设置选中的值
     * @param defaultGrade
     */
    public void setSelect(String defaultGrade) {
        for (BrandBean typeBean: gradeList) {
            if (defaultGrade.equals(typeBean.getId())){
                resultBean = typeBean;
                typeBean.setChecked(true);
            }
        }

    }

	/**
	 * 多选框
	 */
	@SuppressWarnings("deprecation")
	@SuppressLint({ "NewApi", "InflateParams" })
	private void initMutiSelect(){
		LayoutInflater inflater = (LayoutInflater) mContext.getSystemService(Context.LAYOUT_INFLATER_SERVICE);
		assert inflater != null;
		View contentView = inflater.inflate(R.layout.pop_select_grade, null);
		this.setContentView(contentView);
		this.setWidth(LinearLayout.LayoutParams.MATCH_PARENT);
		this.setHeight(LinearLayout.LayoutParams.WRAP_CONTENT);
		this.setOutsideTouchable(true);
		this.setBackgroundDrawable(new BitmapDrawable());
		this.setFocusable(true);
		this.setAnimationStyle(R.style.popwin_top_to_bottom_anim_style);

        //获取自身的长宽高
        contentView.measure(View.MeasureSpec.UNSPECIFIED, View.MeasureSpec.UNSPECIFIED);
        popupHeight = contentView.getMeasuredHeight();
        popupWidth = contentView.getMeasuredWidth();

		View view = contentView.findViewById(R.id.view);
        view.getBackground().setAlpha(120);

		RecyclerView recyclerView = contentView.findViewById(R.id.recyclerView);

        view.setOnClickListener(new OnClickListener() {
			@Override
			public void onClick(View v) {
				SelectBrandPopwidow.this.dismiss();
			}
		});

        mAdapter = new BrandListAdapter(mContext, gradeList);
        mAdapter.setItemClickListener(new BrandListAdapter.OnItemClickListener() {
            @Override
            public void onItemClick(View view, int position) {
                SelectBrandPopwidow.this.dismiss();
                //得到选中的年级ID
//				resultGradeId = gradeList.get(position).getId();

            }
        });
		recyclerView.setLayoutManager(new LinearLayoutManager(mContext));
        recyclerView.setAdapter(mAdapter);
	}


    public void setList(ArrayList<BrandBean> list) {
        this.gradeList = list;
        mAdapter.notifyDataSetChanged();
    }

    public BrandBean getResultBean() {
        return resultBean;
    }

    /**
     * 版本差异性方法，针对7.0版本
     * @param anchor 触发者
     */
    @Override
    public void showAsDropDown(View anchor) {
        if(Build.VERSION.SDK_INT == 24) {
            Rect rect = new Rect();
            anchor.getGlobalVisibleRect(rect);
            int h = anchor.getResources().getDisplayMetrics().heightPixels - rect.bottom;
            setHeight(h);
        }
        super.showAsDropDown(anchor);
    }

    /**
     * 在控件上方弹出
     * @param anchor
     */
    public void showAtLocation(View anchor) {
        int[] location = new int[2];
        anchor.getLocationOnScreen(location);
        //在控件上方显示
        showAtLocation(anchor, Gravity.NO_GRAVITY, (location[0] + anchor.getWidth() / 2) - popupWidth / 2, location[1] - popupHeight);
    }


    public class MyLayoutManager extends LinearLayoutManager {

        Context context;
        int itemNum;

        MyLayoutManager(Context context, int itemNum) {
            super(context);
            this.context = context;
            this.itemNum = itemNum;
        }

        @Override
        public void onMeasure(RecyclerView.Recycler recycler, RecyclerView.State state, int widthSpec, int heightSpec) {
            View view = recycler.getViewForPosition(0);
            if(view != null){
                measureChild(view, widthSpec, heightSpec);
                int measuredWidth = View.MeasureSpec.getSize(widthSpec);
                int measuredHeight = view.getMeasuredHeight() * itemNum;

                // 设置RecyleView的高度为实际高度的一半
                setMeasuredDimension(measuredWidth, measuredHeight / 2);
            }
        }
    }


}
